# Replication archive for: Coppock, Alexander and Seth J. Hill and Lynn Vavreck. 2020. "The Small Effects of Political Advertising are Small Regardless of Context, Message, Sender, or Receiver: Evidence from 59 Real-time Randomized Experiments" Science Advances, forthcoming.

library(tidyverse)
library(estimatr)
library(rmeta)

exps <- read_rds("exps.rds")
ad_df <- read_csv("ad_df.csv")

breaks <-
  as.Date(
    c(
      "2016-04-01",
      "2016-05-01",
      "2016-06-01",
      "2016-07-01",
      "2016-08-01",
      "2016-09-01",
      "2016-10-01",
      "2016-11-01"
    )
  )

exps <-
  exps %>%
  mutate(
    wave_has_ad_id_8 = wave %in% unique(wave[ad_id == 8]),
    wave_has_ad_id_9 = wave %in% unique(wave[ad_id == 9]),
    wave_has_ad_id_11 = wave %in% unique(wave[ad_id == 11]),
    wave_has_ad_id_30 = wave %in% unique(wave[ad_id == 30]),
    wave_has_ad_id_33 = wave %in% unique(wave[ad_id == 33]),
    wave_has_ad_id_47 = wave %in% unique(wave[ad_id == 47]),
    wave_has_ad_id_63 = wave %in% unique(wave[ad_id == 63])
  )

#  8 Quotes        Anti Trump
ad_id_8 <-
  exps %>%
  filter(wave_has_ad_id_8) %>%
  filter(ad_id %in% c(3, 8)) %>%
  group_by(date) %>%
  do(tidy(lm_robust(favorDT_rev ~ as.factor(ad_id) + pid_7_pre + ideo5_pre + female_pre, data = .)))

#  9 Real Life     Pro Clinton
ad_id_9 <-
  exps %>%
  filter(wave_has_ad_id_9) %>%
  filter(ad_id %in% c(3, 9)) %>%
  group_by(date) %>%
  do(tidy(lm_robust(favorDT_rev ~ as.factor(ad_id) + pid_7_pre + ideo5_pre + female_pre, data = .)))

# 11 America       Pro Sanders
ad_id_11 <-
  exps %>%
  filter(wave_has_ad_id_11) %>%
  filter(ad_id %in% c(3, 11)) %>%
  group_by(date) %>%
  do(tidy(lm_robust(favorBS_rev ~ as.factor(ad_id) + pid_7_pre + ideo5_pre + female_pre, data = .)))

# 30 Love/Kindness Pro Clinton
ad_id_30 <-
  exps %>%
  filter(wave_has_ad_id_30) %>%
  filter(ad_id %in% c(3, 30)) %>%
  group_by(date) %>%
  do(tidy(lm_robust(favorHC_rev ~ as.factor(ad_id) + pid_7_pre + ideo5_pre + female_pre, data = .)))


# 33 Speak         Anti Trump
ad_id_33 <-
  exps %>%
  filter(wave_has_ad_id_33) %>%
  filter(ad_id %in% c(3, 33)) %>%
  group_by(date) %>%
  do(tidy(lm_robust(favorDT_rev ~ as.factor(ad_id) + pid_7_pre + ideo5_pre + female_pre, data = .)))

# 47 Role Model    Anti Trump
ad_id_47 <-
  exps %>%
  filter(wave_has_ad_id_47) %>%
  filter(ad_id %in% c(3, 47)) %>%
  group_by(date) %>%
  do(tidy(lm_robust(favorDT_rev ~ as.factor(ad_id) + pid_7_pre + ideo5_pre + female_pre, data = .)))

# 63 Mirrors       Anti Trump
ad_id_63 <-
  exps %>%
  filter(wave_has_ad_id_63) %>%
  filter(ad_id %in% c(3, 63)) %>%
  group_by(date) %>%
  do(tidy(lm_robust(favorDT_rev ~ as.factor(ad_id) + pid_7_pre + ideo5_pre + female_pre, data = .)))

gg_df <-
  bind_rows(
    `8` = ad_id_8,
    `9` = ad_id_9,
    `11` = ad_id_11,
    `30` = ad_id_30,
    `33` = ad_id_33,
    `47` = ad_id_47,
    `63` = ad_id_63,
    .id = "ad_id"
  ) %>%
  filter(grepl("ad_id", term)) %>%
  left_join(mutate(ad_df, ad_id = as.character(ad_id)))


g <-
  ggplot(gg_df, aes(date, estimate)) +
  geom_point() +
  geom_errorbar(aes(ymin = conf.low, ymax = conf.high), width = 0) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  geom_vline(xintercept = as.Date("2016-06-10"), linetype = "dotted") +
  scale_x_date(breaks = breaks, date_labels = "%B") +
  coord_cartesian(ylim = c(-0.6, 0.6)) +
  facet_wrap( ~ ad_title, nrow = 4) +
  theme_bw() +
  ylab("Average treatment effect estimate and 95% confidence interval") +
  theme(
    strip.background = element_blank(),
    axis.title.x = element_blank(),
    panel.grid.minor = element_blank(),
    text = element_text(size = 10),
    axis.text.x = element_text(size = 6)
  ) 

g
